/**
 * Created by lvlq on 16/3/14.
 */
var tr = require('transliteration');

module.exports.index = function (req, res) {
    var id = req.query.id;
    var TopList = M("index.toplist");

    TopList.findAll({
        where: {
            pid: id
        },
        raw: false
    }).then(function (list) {
        var dl = {};
        for (var i = 0; i < list.length; i++) {
            list[i] = list[i].toJSON();
            list[i].nickname = list[i].nickname.replace(/\?/g, "").trim();
            var h = tr(list[i].nickname)[0];
            h = h.toUpperCase();
            if (h == "?" || h == "@") {
                h = "#";
            }
            dl[h] = dl[h] || [];
            dl[h].push(list[i]);
        }
        var arr = [];
        for (var key in dl) {
            arr.push({
                word: key,
                list: dl[key]
            });
        }

        arr.sort(function (a, b) {
            if (a.word == "#") {
                return 1;
            }
            if (b.word == "#") {
                return -1;
            }
            return a.word > b.word ? 1 : -1
        });
        // console.log(arr);
        res.render("toplist.html", {
            id: id,
            list: list,
            arr: arr
        })
    }).catch(function (err) {
        console.log(err.message);
        res.send("系统错误,请联系管理员~~");
    });
};


module.exports.menu = function (req, res) {
    res.render("menu.html");
};